package com.lagou.niancheng.dubbo.filter;

import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;

public class TransportIPFilter implements Filter {

    private static final String IP_ATTACHMENT_NAME = "ipAttachment";

    public static void setIp(String ip) {
        RpcContext.getContext().setAttachment(IP_ATTACHMENT_NAME, ip);
    }

    public static String getIp() {
        return RpcContext.getContext().getAttachment(IP_ATTACHMENT_NAME);
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        System.out.println("捕获到请求者ip：" + getIp());
        return invoker.invoke(invocation);
    }
}
